﻿using KYJScadaProModbusRtu;
using SeeSharpTools.JY.GUI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KYJScadaProModbusRTU
{
    public partial class FrmActualTrend : Form
    {
        public FrmActualTrend()
        {
            InitializeComponent();

            //通过代码设置chart属性

            //x轴的数据类型
            this.chart_ActualTrend.XDataType = StripChartX.XAxisDataType.TimeStamp;
            this.chart_ActualTrend.TimeStampFormat = "HH:mm:ss";

            //设置图例
            this.chart_ActualTrend.LegendVisible = false;

            //设置显示的数据点
            this.chart_ActualTrend.DisplayPoints = 4000;

            //Y轴的范围
            this.chart_ActualTrend.AxisY.Minimum = 0.0f;
            this.chart_ActualTrend.AxisY.Maximum = 50.0;
            this.chart_ActualTrend.AxisY.AutoScale = false;

            if (CommomMethods.plcDevice != null)
            {
                //获取归档变量的数量
                int total = CommomMethods.plcDevice.StoreVarList.Count;
                this.chart_ActualTrend.Series.Clear();

                //设置曲线的数量
                this.chart_ActualTrend.SeriesCount = total;

                //设置曲线
                for (int i = 0; i < total; i++)
                {
                    //设置曲线的名称
                    this.chart_ActualTrend.Series[i].Name = CommomMethods.plcDevice.StoreVarList[i].Comments;

                    //设置曲线不可见
                    this.chart_ActualTrend.Series[i].Visible = false;

                    //设置曲线的粗细
                    this.chart_ActualTrend.Series[i].Width = StripChartXSeries.LineWidth.Middle;

                    //设置每条曲线的Y轴是Y1/Y2
                    if (this.chart_ActualTrend.Series[i].Name.Contains("温度"))
                    {
                        this.chart_ActualTrend.Series[i].YPlotAxis = StripChartXAxis.PlotAxis.Primary;
                    }
                    else
                    {
                        this.chart_ActualTrend.Series[i].YPlotAxis = StripChartXAxis.PlotAxis.Secondary;
                    }
                }

                //设置参数
                this.MainpPanel.Controls.Clear();
                int rowGap = 40;
                int colGap = 175;

                int xStart = 19;
                int yStart = 11;

                for (int i = 0; i < total; i++)
                {
                    //创建一个CheckBox控件对象
                    CheckBox checkBox = new CheckBox();

                    //设置基础属性
                    checkBox.AutoSize = true;
                    checkBox.ForeColor = Color.White;
                    checkBox.Name = "chk" + i.ToString();
                    checkBox.Text = CommomMethods.plcDevice.StoreVarList[i].Comments;

                    checkBox.Tag = i;
                    checkBox.CheckedChanged += CheckBox_CheckedChanged;

                    //设置Location
                    int rowIndex = i / 6;
                    int colIndex = i % 6;
                    checkBox.Location = new Point(xStart + colIndex * colGap, yStart + rowIndex * rowGap);

                    //设置Checked
                    checkBox.Checked = i < CommomMethods.sysConfig.ShowSeriesCount;

                    //添加到Panal
                    this.MainpPanel.Controls.Add(checkBox);
                }

                //开启定时器
                this.updateTimer = new Timer();
                this.updateTimer.Interval = 1000;
                this.updateTimer.Tick += UpdateTimer_Tick;
                this.updateTimer.Start();

                //FormClosing事件关闭定时器
                this.FormClosing += (sender, e) =>
                {
                    this.updateTimer.Stop();
                };
            }
        }

        //实时更新的定时器
        private Timer updateTimer;

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            if (CommomMethods.connectState)
            {
                List<double> yData = new List<double>();
                foreach (var item in CommomMethods.plcDevice.StoreVarList)
                {
                    string varValue = item.VarValue?.ToString();
                    if (double.TryParse(varValue, out double value))
                    {
                        yData.Add(value);
                    }
                    else
                    {
                        yData.Add(0.0);
                    }

                }
                this.chart_ActualTrend.PlotSingle(yData.ToArray());
            }
        }

        private void CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if (sender is CheckBox checkBox)
            {
                if (checkBox.Tag != null && checkBox.Tag.ToString().Length > 0)
                {
                    int index = Convert.ToInt32(checkBox.Tag.ToString());
                    this.chart_ActualTrend.Series[index].Visible = checkBox.Checked;
                }
            }
        }

        #region 减少闪烁
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams parss = base.CreateParams;
                parss.ExStyle |= 0x02000000;
                return parss;
            }
        }
        #endregion
    }
}
